package cn.gjculture.shopping.api.service;

import cn.gjculture.shopping.api.entity.Customer;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.SmsUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class SmsService {

    @Autowired
    CustomerService customerService;

    Map<String, Code> codes = new ConcurrentHashMap<>();

    public boolean sendAuthCode(String phone) {
        String code = getCode();
        try {
            SendSmsResponse sendSmsResponse = SmsUtil.sendSms(phone, code);
            if (!sendSmsResponse.getCode().equals("OK")) {
                log.warn("短信发送失败,失败信息:{}", sendSmsResponse.getMessage());
                return false;
            }
            this.putCode(phone, code);
        } catch (ClientException e) {
            e.printStackTrace();
            log.warn("短信发送失败,失败信息:{}", e.getMessage());
            return false;
        }
        return true;
    }



    private void putCode(String phone, String code) {
        Long time = System.currentTimeMillis();
        Code c = new Code(phone, code, time + 90000);
        if (codes.containsKey(phone)) {
            codes.remove(phone);
        }
        codes.put(phone, c);
    }

    private Code getCode(String phone) {
        if (phone != null) {
            Code c = codes.get(phone);
            if (c!=null&&c.getTime() < System.currentTimeMillis()) {
                codes.remove(phone);
                return null;
            }else {
                return c;
            }
        }
        return null;
    }


    public static String getCode() {
        String[] numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "8"};
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int index = (int) (Math.random() * 10);
            code.append(numbers[index]);
        }
        return code.toString();

    }

    public boolean verifyCode(String id, String phone, String code) {
        Code c=getCode(phone);
        if (c!=null&&c.getCode().equals(code)) {
            codes.remove(phone);
            Customer customer=new Customer();
            customer.setId(id);
            customer.setPhone(phone);
            customer.setPhoneVerified(0);
            customer.setPhoneVerifiedTime(DateUtil.getTimestamp());
            customerService.setPhoneVerify(customer);
            return true;
        }
        return false;
    }


    @Data
    class Code {
        private String phone;
        private String code;
        private long time;

        public Code(String phone, String code, long time) {
            this.phone = phone;
            this.code = code;
            this.time = time;
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(getCode());
        }
    }
}
